// pages/i_offer/i_offer.js
const app = getApp()
var flag = 1;var save_order_offer;var save_offer_img;var get_user;

function uploadimg(data) {
  console.log(data);
  var that = this,
    i = data.i ? data.i : 0,
    success = data.success ? data.success : 0,
    fail = data.fail ? data.fail : 0;
  wx.uploadFile({
    url: data.url,
    filePath: data.path[i],
    name: 'file',
    formData: {
      id: data.id,
      type: data.type
    },
    success: function (res) {
      success++;
      console.log(res)
      console.log(i);
      console.log(data.id);
    },
    fail: function (res) {
      fail++;
      console.log('fail:' + i + "fail:" + fail);
    },
    complete: function (res) {
      console.log(i);
      i++;
      if (i == data.path.length) {   //当图片传完时，停止调用   
        console.log('执行完毕');
        console.log('成功：' + success + " 失败：" + fail);
      } else {
        console.log(i);
        data.i = i;
        data.success = success;
        data.fail = fail;
        uploadimg(data);
      }

    }
  });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['请选择', '当日达', '次日达','两天达','三天达','四天达','五天达'],
    index:0,
    service:[
      {name:'上门接宠',select:false},
      {name:'送宠到家',select:false},
      {name:'需宠具',select:false}
    ],
    array1: ['请选择', '宠物专车', '汽车托运', '铁路托运', '空运', '混合运'],
    index1:0,
    select:false,
    price:'',
    remark:'',
    tempFilePaths:[],
    smjc:'',
    scdj:'',
    lj:'',
    oid:'',
    type:'',
  },
  pricetap:function(e){
    this.setData({
      price:e.detail.value
    })
  },
  remarktap:function(e){
    this.setData({
      remark:e.detail.value
    })
  },
  xztap:function(e){
    this.setData({
      select:!this.data.select
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    flag = 1;
    var oid = options.oid;
    save_order_offer = app.data.index_url + 'save_order_offer';
    save_offer_img = app.data.index_url + 'save_offer_img';
    get_user = app.data.ucenter_url + 'get_user';
    that.setData({
      oid:oid
    })
    var aid = wx.getStorageSync('uid');
    wx.request({
      url: get_user,
      data: {
        uid: aid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        var info = res.data.info;
        var role_id = info.role_id;
        that.setData({
          type:role_id
        })
      }
    })
  },
  xytap:function(e){
    wx.navigateTo({
      url: '/pages/i_xy/i_xy?name=sjxy',
    })
  },
  imgdeltap:function(e){
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;
    tempFilePaths.splice(index,1);
    that.setData({
      tempFilePaths:tempFilePaths
    })
  },
  chooseimage: function (e) {
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;
    var img = tempFilePaths;
    wx.chooseImage({
      count: 6, // 默认9  
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        for (var i = 0; i < imgsrc.length; i++) {
          img.push(imgsrc[i])
        }
        console.log(img);
        that.setData({
          tempFilePaths: img
        })
      }
    })
  },
  sertap:function(e){
    var that = this;
    var service = that.data.service;
    var index = e.currentTarget.dataset.index;
    if(index == 0 && service[index].select == false){
      that.setData({
        smjc:service[index].name
      })
    }
    if(index == 0 && service[index].select == true){
      that.setData({
        smjc:''
      })
    }
    if(index == 1 && service[index].select == false){
      that.setData({
        scdj:service[index].name
      })
    }
    if(index == 1 && service[index].select == true){
      that.setData({
        scdj:''
      })
    }
    if(index == 2 && service[index].select == false){
      that.setData({
        lj:service[index].name
      })
    }
    if(index == 2 && service[index].select == true){
      that.setData({
        lj:''
      })
    }
    service[index].select = !service[index].select;
    that.setData({
      service:service
    })
  },
  xztap:function(e){
    var that = this;
    var xz = that.data.select
    that.setData({
      select:!xz
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  },
  submit:function(e){
    var that = this;
    if(flag == 1){
      flag = 0;
      var array = that.data.array;
      var index = that.data.index;
      var stime = array[index];
      if(index == 0){
        wx.showModal({
          title: '提示',
          content: '请选择送达时间',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var smjc = that.data.smjc;
      var scdj = that.data.scdj;
      var lj = that.data.lj;
      // if(smjc == '' && scdj == '' && lj == ''){
      //   wx.showModal({
      //     title: '提示',
      //     content: '请选择接送服务',
      //     showCancel: false
      //   })
      //   flag = 1;
      //   return false;
      // }
      var array1 = that.data.array1;
      var index1 = that.data.index1;
      var ps_way = array1[index1];
      if(index1 == 0){
        wx.showModal({
          title: '提示',
          content: '请选择托运方式',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var price = that.data.price;
      if(price == ''){
        wx.showModal({
          title: '提示',
          content: '请输入运费报价',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var remark = that.data.remark;
      var tempFilePaths = that.data.tempFilePaths;
      var select = that.data.select;
      if(select == false){
        wx.showModal({
          title: '提示',
          content: '请阅读并同意《服务条款》',
          showCancel: false
        })
        flag = 1;
        return false;
      }
      var aid = wx.getStorageSync('uid');
      wx.showLoading();
      wx.request({
        url: save_order_offer,
        data: {
          uid:aid,
          oid:that.data.oid,
          price:price,
          type:that.data.type,
          stime:stime,
          remark:remark,
          ps_way:ps_way,
          smjc:smjc,
          scdj:scdj,
          lj:lj
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data);
          if(res.data.state == 1){
            var id = res.data.id;
            if(tempFilePaths.length != 0){
              uploadimg({
                url: save_offer_img,//这里是你图片上传的接口
                path: tempFilePaths,//这里是选取的图片的地址数组
                id: id
              });
            }
            flag = 1;
            setTimeout(function(e){
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '报价信息已提交！',
                showCancel: false,
                success: function (e) {
                  wx.navigateBack({
                    delta: 2,
                  })
                }
              })
            },500)
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})